

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.nn.optim.asgd &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.nn.optim.asgd</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.nn.optim.asgd</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;asgd&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">functional</span> <span class="k">as</span> <span class="n">F</span><span class="p">,</span> <span class="n">operations</span> <span class="k">as</span> <span class="n">P</span>
<span class="kn">from</span> <span class="nn">mindspore.common.parameter</span> <span class="kn">import</span> <span class="n">Parameter</span>
<span class="kn">from</span> <span class="nn">mindspore.common.tensor</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="kn">import</span> <span class="nn">mindspore.common.dtype</span> <span class="k">as</span> <span class="nn">mstype</span>
<span class="kn">import</span> <span class="nn">mindspore</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span> <span class="k">as</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">.optimizer</span> <span class="kn">import</span> <span class="n">Optimizer</span>
<span class="kn">from</span> <span class="nn">.optimizer</span> <span class="kn">import</span> <span class="n">opt_init_args_register</span>


<div class="viewcode-block" id="ASGD"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.ASGD.html#mindspore.nn.ASGD">[docs]</a><span class="k">class</span> <span class="nc">ASGD</span><span class="p">(</span><span class="n">Optimizer</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Implements Average Stochastic Gradient Descent.</span>

<span class="sd">    Introduction to ASGD can be found at `Acceleration of stochastic approximation by average</span>
<span class="sd">    &lt;http://dl.acm.org/citation.cfm?id=131098&gt;`_.</span>

<span class="sd">    The updating formulas are as follows:</span>

<span class="sd">    .. math::</span>
<span class="sd">        \begin{gather*}</span>
<span class="sd">            w_{t} = w_{t-1} * (1 - \lambda * \eta_{t-1}) - \eta_{t-1} * g_{t} \\</span>
<span class="sd">            ax_{t} = (w_t - ax_{t-1}) * \mu_{t-1} \\</span>
<span class="sd">            \eta_{t} = \frac{1.}{(1 + \lambda * lr * t)^\alpha} \\</span>
<span class="sd">            \mu_{t} = \frac{1}{\max(1, t - t0)}</span>
<span class="sd">        \end{gather*}</span>

<span class="sd">    :math:`\lambda` represents the decay term, :math:`\mu` and :math:`\eta` are tracked to</span>
<span class="sd">    update :math:`ax` and :math:`w`, :math:`t0` represents the point of starting averaging,</span>
<span class="sd">    :math:`\alpha` represents the power for eta update, :math:`ax` represents the averaged</span>
<span class="sd">    parameter value, :math:`t` represents the current step, :math:`g` represents `gradients`,</span>
<span class="sd">    :math:`w` represents `params`.</span>

<span class="sd">    Note:</span>
<span class="sd">        If parameters are not grouped, the `weight_decay` in optimizer will be applied on the parameters without &#39;beta&#39;</span>
<span class="sd">        or &#39;gamma&#39; in their names. Users can group parameters to change the strategy of decaying weight. When parameters</span>
<span class="sd">        are grouped, each group can set `weight_decay`, if not, the `weight_decay` in optimizer will be applied.</span>

<span class="sd">    Args:</span>
<span class="sd">        params (Union[list[Parameter], list[dict]]): Must be list of `Parameter` or list of `dict`. When the</span>
<span class="sd">            `parameters` is a list of `dict`, the &quot;params&quot;, &quot;lr&quot;, &quot;weight_decay&quot;, &quot;grad_centralization&quot; and</span>
<span class="sd">            &quot;order_params&quot; are the keys can be parsed.</span>

<span class="sd">            - params: Required. Parameters in current group. The value must be a list of `Parameter`.</span>

<span class="sd">            - lr: Optional. If &quot;lr&quot; in the keys, the value of corresponding learning rate will be used.</span>
<span class="sd">              If not, the `learning_rate` in optimizer will be used. Fixed and dynamic learning rate are supported.</span>

<span class="sd">            - weight_decay: Optional. If &quot;weight_decay&quot; in the keys, the value of corresponding weight decay</span>
<span class="sd">              will be used. If not, the `weight_decay` in the optimizer will be used.</span>

<span class="sd">            - grad_centralization: Optional. Must be Boolean. If &quot;grad_centralization&quot; is in the keys, the set value</span>
<span class="sd">              will be used. If not, the `grad_centralization` is False by default. This configuration only works on the</span>
<span class="sd">              convolution layer.</span>

<span class="sd">            - order_params: Optional. When parameters is grouped, this usually is used to maintain the order of</span>
<span class="sd">              parameters that appeared in the network to improve performance. The value should be parameters whose</span>
<span class="sd">              order will be followed in optimizer.</span>
<span class="sd">              If `order_params` in the keys, other keys will be ignored and the element of &#39;order_params&#39; must be in</span>
<span class="sd">              one group of `params`.</span>

<span class="sd">        learning_rate (Union[float, int, Tensor, Iterable, LearningRateSchedule]):</span>

<span class="sd">            - float: The fixed learning rate value. Must be equal to or greater than 0.</span>

<span class="sd">            - int: The fixed learning rate value. Must be equal to or greater than 0. It will be converted to float.</span>

<span class="sd">            - Tensor: Its value should be a scalar or a 1-D vector. For scalar, fixed learning rate will be applied.</span>
<span class="sd">              For vector, learning rate is dynamic, then the i-th step will take the i-th value as the learning rate.</span>

<span class="sd">            - Iterable: Learning rate is dynamic. The i-th step will take the i-th value as the learning rate.</span>

<span class="sd">            - LearningRateSchedule: Learning rate is dynamic. During training, the optimizer calls the instance of</span>
<span class="sd">              LearningRateSchedule with step as the input to get the learning rate of current step.</span>

<span class="sd">        lambd (float): The decay term. Default: 1e-4.</span>
<span class="sd">        alpha (float): The power for eta update. Default: 0.75.</span>
<span class="sd">        t0 (float): The point of starting averaging. Default: 1e6.</span>
<span class="sd">        weight_decay (int, float): Weight decay (L2 penalty). It must be equal to or greater than 0. Default: 0.0.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **gradients** (tuple[Tensor]) - The gradients of `params`, the shape is the same as `params`.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor[bool], the value is True.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `learning_rate` is not one of int, float, Tensor, Iterable, LearningRateSchedule.</span>
<span class="sd">        TypeError: If element of `parameters` is neither Parameter nor dict.</span>
<span class="sd">        TypeError: If `lambd`, `alpha` or `t0` is not a float.</span>
<span class="sd">        TypeError: If `weight_decay` is neither float nor int.</span>
<span class="sd">        ValueError: If `weight_decay` is less than 0.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import nn, Model</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; net = Net()</span>
<span class="sd">        &gt;&gt;&gt; #1) All parameters use the same learning rate and weight decay</span>
<span class="sd">        &gt;&gt;&gt; optim = nn.ASGD(params=net.trainable_params())</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; #2) Use parameter groups and set different values</span>
<span class="sd">        &gt;&gt;&gt; conv_params = list(filter(lambda x: &#39;conv&#39; in x.name, net.trainable_params()))</span>
<span class="sd">        &gt;&gt;&gt; no_conv_params = list(filter(lambda x: &#39;conv&#39; not in x.name, net.trainable_params()))</span>
<span class="sd">        &gt;&gt;&gt; group_params = [{&#39;params&#39;: conv_params,&#39;grad_centralization&#39;:True},</span>
<span class="sd">        ...                 {&#39;params&#39;: no_conv_params, &#39;lr&#39;: 0.01},</span>
<span class="sd">        ...                 {&#39;order_params&#39;: net.trainable_params()}]</span>
<span class="sd">        &gt;&gt;&gt; optim = nn.ASGD(group_params, learning_rate=0.1, weight_decay=0.0)</span>
<span class="sd">        &gt;&gt;&gt; # The conv_params&#39;s parameters will use default learning rate of 0.1 default weight decay of 0.0 and grad</span>
<span class="sd">        &gt;&gt;&gt; # centralization of True.</span>
<span class="sd">        &gt;&gt;&gt; # The no_conv_params&#39;s parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad</span>
<span class="sd">        &gt;&gt;&gt; # centralization of False.</span>
<span class="sd">        &gt;&gt;&gt; # The final parameters order in which the optimizer will be followed is the value of &#39;order_params&#39;.</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; loss = nn.SoftmaxCrossEntropyWithLogits()</span>
<span class="sd">        &gt;&gt;&gt; model = Model(net, loss_fn=loss, optimizer=optim)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@opt_init_args_register</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">learning_rate</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">lambd</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.75</span><span class="p">,</span> <span class="n">t0</span><span class="o">=</span><span class="mf">1e6</span><span class="p">,</span> <span class="n">weight_decay</span><span class="o">=</span><span class="mf">0.</span><span class="p">):</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">ASGD</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">learning_rate</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">weight_decay</span><span class="p">)</span>

        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;lambd&quot;</span><span class="p">,</span> <span class="n">lambd</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;t0&quot;</span><span class="p">,</span> <span class="n">t0</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">lambd</span> <span class="o">=</span> <span class="n">lambd</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">alpha</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t0</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([</span><span class="n">t0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
        <span class="n">mu</span><span class="p">,</span> <span class="n">eta</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="p">:</span>
            <span class="n">mu</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Parameter</span><span class="p">(</span><span class="n">Tensor</span><span class="p">(</span><span class="mf">1.</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;mu_&#39;</span><span class="o">+</span><span class="n">param</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
            <span class="n">eta</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Parameter</span><span class="p">(</span><span class="n">Tensor</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;eta_&#39;</span><span class="o">+</span><span class="n">param</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lens</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mu</span> <span class="o">=</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">ParameterTuple</span><span class="p">(</span><span class="n">mu</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eta</span> <span class="o">=</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">ParameterTuple</span><span class="p">(</span><span class="n">eta</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">step</span> <span class="o">=</span> <span class="n">Parameter</span><span class="p">(</span><span class="n">Tensor</span><span class="p">(</span><span class="mf">1.</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;step&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ax</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s2">&quot;ax_&quot;</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="s1">&#39;zeros&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pow</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Pow</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">maximum</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Maximum</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assign</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Assign</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assignadd</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">AssignAdd</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assignsub</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">AssignSub</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cast</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Cast</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">squeeze</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Squeeze</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gradients</span><span class="p">):</span>
        <span class="n">gradients</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">decay_weight</span><span class="p">(</span><span class="n">gradients</span><span class="p">)</span>
        <span class="n">gradients</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gradients_centralization</span><span class="p">(</span><span class="n">gradients</span><span class="p">)</span>
        <span class="n">gradients</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scale_grad</span><span class="p">(</span><span class="n">gradients</span><span class="p">)</span>
        <span class="n">lrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_lr</span><span class="p">()</span>
        <span class="n">success</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="p">(</span><span class="n">grad</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">eta</span><span class="p">,</span> <span class="n">ax</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">gradients</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mu</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">eta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ax</span><span class="p">)):</span>
            <span class="n">lr</span> <span class="o">=</span> <span class="n">lrs</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_group_lr</span> <span class="k">else</span> <span class="n">lrs</span>
            <span class="n">lr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">lr</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">step</span> <span class="o">==</span> <span class="mf">1.</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">eta</span><span class="p">,</span> <span class="n">lr</span><span class="p">)</span>

            <span class="n">param_fp32</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">param</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
            <span class="n">gradient_fp32</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">grad</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
            <span class="n">ax_fp32</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
            <span class="n">param_fp32</span> <span class="o">=</span> <span class="n">param_fp32</span> <span class="o">*</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">lambd</span> <span class="o">*</span> <span class="n">eta</span><span class="p">)</span> <span class="o">-</span> <span class="n">eta</span> <span class="o">*</span> <span class="n">gradient_fp32</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">param</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">param_fp32</span><span class="p">,</span> <span class="n">param</span><span class="o">.</span><span class="n">dtype</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">mu</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">assignadd</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cast</span><span class="p">((</span><span class="n">param_fp32</span> <span class="o">-</span> <span class="n">ax_fp32</span><span class="p">)</span> <span class="o">*</span> <span class="n">mu</span><span class="p">,</span> <span class="n">ax</span><span class="o">.</span><span class="n">dtype</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">param</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">eta</span><span class="p">,</span> <span class="n">lr</span> <span class="o">/</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pow</span><span class="p">((</span><span class="mf">1.</span> <span class="o">+</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lambd</span> <span class="o">*</span> <span class="n">lr</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">step</span><span class="p">)),</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="mf">1.</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="mf">1.</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">step</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">t0</span><span class="p">)))</span>

        <span class="n">success</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">depend</span><span class="p">(</span><span class="n">success</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">assignadd</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">step</span><span class="p">,</span> <span class="mf">1.</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">success</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>